import {
  reqDeviceList,
  reqProductList,
  reqMaintenancepage
} from "../../../request/api"
import {
  showMsg
} from '../../../utils/config.js'

var app = getApp()
Page({
  data: {
    navHeight: app.globalData.navHeight + 'px',
    online: '',
    productId: '',
    spaceId: '',
    searchValue: '',
    minBattery: '',
    maxBattery: '',
    isAlarm: '',
    page: 1,
    isNextPage: true,
    triggered: false,
    dataList: [],
    productList: [],
    maintenanceId: '',
    showSearch: false, //筛选弹窗
    statusList: [{
      id: '',
      name: '全部',
      active: true,
    }, {
      id: 1,
      name: '在线',
      active: false,
    }, {
      id: 0,
      name: '离线',
      active: false,
    }, ],
    isAlarmList: [{
      active: false,
      name: "正常",
      id: 0
    }, {
      active: false,
      name: "告警",
      id: 1
    }],
    name: "",
    maintenanceName: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(e) {
    console.log(e);
    this.setData({
      info: JSON.parse(e.info),
    })
    // await this.getDataList()
    this.getMaintenancepage()
  },
  // 维保组
  async getMaintenancepage() {
    let params = {
      orgId: this.data.info.orgId,
      name: this.data.name,
    }
    const res = await reqMaintenancepage(this.data.page, 14, params)
    if (res.code !== 20000) return showMsg(res.message)
    console.log(res);
    let {
      rows
    } = res.data
    if (this.data.page === 1) {
      this.setData({
        dataList: rows,
        isNextPage: true
      })
    } else if (this.data.page > 1 && rows.length) {
      this.setData({
        dataList: this.data.dataList.concat(rows),
        isNextPage: true
      })
    } else if (this.data.page > 1 && !rows.length) {
      showMsg('已经到底啦~')
      this.setData({
        isNextPage: false
      })
    }
  },

  async getDataList() {
    let params = {
      orgId: this.data.info.orgId,
      online: this.data.online,
      productId: this.data.productId,
      spaceId: this.data.spaceId,
      imei: this.data.searchValue,
      minBattery: this.data.minBattery,
      maxBattery: this.data.maxBattery,
      isAlarm: this.data.isAlarm,
      userId: app.globalData.userId,
      roleName: app.globalData.rolename,
    }
    const res = await reqDeviceList(this.data.page, 10, params)
    if (res.code !== 20000) return showMsg(res.message)
    let {
      rows
    } = res.data
    let {
      productList
    } = this.data
    if (rows && rows.length) {
      rows.forEach(item => {
        productList.forEach(citem => {
          if (item.deviceType === citem.deviceType) {
            item.type = citem.name
            item.pic = citem.pic
          }
        })
      });
    }
    if (this.data.page === 1) {
      this.setData({
        dataList: rows,
        isNextPage: true
      })
    } else if (this.data.page > 1 && rows.length) {
      this.setData({
        dataList: this.data.dataList.concat(rows),
        isNextPage: true
      })
    } else if (this.data.page > 1 && !rows.length) {
      showMsg('已经到底啦~')
      this.setData({
        isNextPage: false
      })
    }
  },
  // 滚动到底部触发
  lower() {
    if (!this.data.isNextPage) return
    this.setData({
      page: this.data.page + 1,
    })
    this.getMaintenancepage()
  },
  // 下拉刷新
  async onRefresh() {
    this.setData({
      dataList: [],
      page: 1,
      isNextPage: true,
      triggered: true,
    })
    await this.getMaintenancepage()
    this.setData({
      triggered: false,
    })
  },
  // 复选框
  onChange(e) {
    console.log(e.detail);

    let maintenanceName = ''
    this.data.dataList.forEach(item => {
      if (item.id == e.detail) {
        maintenanceName = item.name
      }
    })
    this.setData({
      maintenanceId: e.detail,
      maintenanceName: maintenanceName
    });

  },
  onPop(e) {
    console.log(e.currentTarget.dataset.item);
    let maintenanceName = ''
    this.data.dataList.forEach(item => {
      if (item.id == e.currentTarget.dataset.item.id) {
        maintenanceName = item.name
      }
    })
    this.setData({
      maintenanceId: e.currentTarget.dataset.item.id,
      maintenanceName: maintenanceName
    });
  },
  // 点击筛选
  // clickSearch() {
  //     this.setData({
  //         showSearch: true
  //     })
  // },
  // 点击关闭筛选
  closeSearch() {
    this.setData({
      showSearch: false
    })
  },
  // 重置筛选
  resetSearch() {
    let {
      statusList,
      productList,
      isAlarmList
    } = this.data
    statusList.forEach(item => {
      if (item.id === '') {
        item.active = true
      } else {
        item.active = false
      }
    })
    productList.forEach(item => {
      if (item.id === '') {
        item.active = true
      } else {
        item.active = false
      }
    })
    isAlarmList.forEach(item => {
      item.active = false
    })
    this.setData({
      statusList,
      online: '',
      productList,
      isAlarmList,
      isAlarm: "",
      productId: '',
      minBattery: "",
      maxBattery: ""
    })
    this.confirmSearch()
  },
  // 确定筛选点击
  confirmSearch() {
    let {
      online,
      productId,
      isAlarm,
      minBattery,
      maxBattery
    } = this.data;
    let hasSearch = false;
    hasSearch = ((typeof (online) === 'number') || productId || isAlarm || minBattery || maxBattery) ? true : false
    this.setData({
      showSearch: false,
      hasSearch,
      page: 1,
      isNextPage: true,
      dataList: []
    })
    this.getMaintenancepage()
  },
  // 点击产品类型
  typeClick(e) {
    let {
      id
    } = e.currentTarget.dataset
    let {
      productList
    } = this.data
    productList.forEach(item => {
      if (item.id === id) {
        item.active = true
      } else {
        item.active = false
      }
    })
    this.setData({
      productList,
      productId: id
    })
  },
  // 点击设备状态
  statusClick(e) {
    let {
      id
    } = e.currentTarget.dataset
    let {
      statusList
    } = this.data
    statusList.forEach(item => {
      if (item.id === id) {
        item.active = true
      } else {
        item.active = false
      }
    })
    this.setData({
      statusList,
      online: id
    })
  },
  // 点击告警状态
  isAlarmClick(e) {
    let {
      id
    } = e.currentTarget.dataset;
    console.log(id);
    let {
      isAlarmList
    } = this.data
    isAlarmList.forEach(item => {
      if (item.id === id) {
        item.active = true
      } else {
        item.active = false
      }
    })
    this.setData({
      isAlarmList,
      isAlarm: id
    })
  },
  // 操作列表后回到最初状态
  getInitDataList() {
    this.setData({
      page: 1,
      isNextPage: true,
      dataList: []
    })
    this.getMaintenancepage()
  },
  // 输入框搜索
  searchChange(e) {
    // e.detail = e.detail.replace(/[^0-9]/g, '');
    this.setData({
      name: e.detail,
    })
    this.data.timer && clearTimeout(this.data.timer)
    this.data.timer = setTimeout(() => {
      this.getInitDataList()
    }, 500)
  },

  onSave() {
    wx.setStorageSync('maintenanceId', this.data.maintenanceId)
    wx.setStorageSync('maintenanceName', this.data.maintenanceName)
    wx.navigateBack()
    // wx.redirectTo({
    //   url: `/pages/index-inside-page/hiddenDanger/hiddenDanger?maintenanceId=${JSON.stringify(this.data.maintenanceId)}`,
    // })
  }
})